//
//  Version.swift
//  Signup
//
//  Created by heze on 2018/10/9.
//  Copyright © 2018 apple. All rights reserved.
//

import Foundation

class Unique: NSObject {
    
    
}

struct Version<Value>: Hashable {
    
    private let _unique: Unique
    let value: Value
    
    init(_ value: Value) {
        self._unique = Unique()
        self.value = value
    }
    
    var hashValue: Int {
        return self._unique.hash
    }
    
    static func == (lhs: Version<Value>, rhs: Version<Value>) -> Bool {
        return lhs._unique === rhs._unique
    }
}

extension Version {
    
    func mutate(transform: (inout Value) -> ()) -> Version<Value> {
        var newSelf = self.value
        transform(&newSelf)
        return Version(newSelf)
    }
    
    func mutate(transform: (inout Value) throws -> ()) rethrows -> Version<Value> {
        var newSelf = self.value
        try transform(&newSelf)
        return Version(newSelf)
    }
    
}
